import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
import { FormItemType } from '@/types/component/form'
import type { FormItemConfig, FormRowItem } from '@/types/component/form'
import type { Ref } from 'vue'

/**
 * 获取用户表单配置项
 * @param formMode 表单模式（add新增/edit编辑）
 * @param deptTree 部门树数据
 * @param roleOptions 角色选项数据
 * @returns 用户表单配置项
 */
export const useUserFormItems = (
  formMode: Ref<'add' | 'edit'>,
  deptTree: Ref<any[]>,
  roleOptions: Ref<any[]>
) => {
  const { t } = useI18n()
  
  return computed<(FormItemConfig | FormRowItem)[]>(() => [
    {
      field: 'username',
      label: t('user.username'),
      type: FormItemType.INPUT,
      placeholder: t('user.usernamePlaceholder'),
      disabled: formMode.value === 'edit' // 编辑模式下用户名不可修改
    },
    {
      field: 'nickname',
      label: t('user.nickname'),
      type: FormItemType.INPUT,
      placeholder: t('user.nicknamePlaceholder')
    },
    {
      field: 'password',
      label: t('user.password'),
      type: FormItemType.INPUT_PASSWORD,
      placeholder: t('user.passwordPlaceholder'),
      condition: formMode.value === 'add' // 只在新增模式下显示密码字段
    },
    {
      field: 'deptId',
      label: t('user.dept'),
      type: FormItemType.TREE_SELECT,
      placeholder: t('user.selectDeptPlaceholder'),
      treeData: deptTree.value,
      fieldNames: { label: 'name', value: 'id', children: 'children' },
    },
    {
      field: 'status',
      label: t('user.status'),
      type: FormItemType.RADIO_GROUP,
      options: [
        { label: t('user.enabled'), value: 1 },
        { label: t('user.disabled'), value: 0 }
      ],
      value: 1
    },
    {
      field: 'phone',
      label: t('user.phone'),
      type: FormItemType.INPUT,
      placeholder: t('user.phonePlaceholder'),
    },
    {
      field: 'email',
      label: t('user.email'),
      type: FormItemType.INPUT,
      placeholder: t('user.emailPlaceholder'),
    },
    {
      field: 'roleIds',
      label: t('user.roles'),
      type: FormItemType.SELECT,
      placeholder: t('user.selectRolesPlaceholder'),
      options: roleOptions.value,
      mode: 'multiple'
    },
    {
      field: 'remark',
      label: t('user.remark'),
      type: FormItemType.TEXTAREA,
      placeholder: t('user.remarkPlaceholder'),
      rows: 4
    }
  ])
} 